/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.itsoo.elasticbatis.parsing.druid.util;

import lombok.extern.slf4j.Slf4j;
import me.itsoo.elasticbatis.parsing.druid.DbType;

import java.io.Closeable;
import java.io.InputStream;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLRecoverableException;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.Properties;

/**
 * @author wenshao [szujobs@hotmail.com]
 */
@Slf4j
public final class JdbcUtils {

    private static final Properties DRIVER_URL_MAPPING = new Properties();

    static {
        try {
            ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader();
            if (ctxClassLoader != null) {
                for (Enumeration<URL> e = ctxClassLoader.getResources("META-INF/druid-driver.properties"); e.hasMoreElements(); ) {
                    URL url = e.nextElement();
                    Properties property = new Properties();
                    InputStream is = null;
                    try {
                        is = url.openStream();
                        property.load(is);
                    } finally {
                        JdbcUtils.close(is);
                    }
                    DRIVER_URL_MAPPING.putAll(property);
                }
            }
        } catch (Exception e) {
            log.error("load druid-driver.properties error", e);
        }
    }

    public static void close(Connection x) {
        if (x == null) {
            return;
        }
        try {
            if (x.isClosed()) {
                return;
            }
            x.close();
        } catch (SQLRecoverableException e) {
            // skip
        } catch (Exception e) {
            log.debug("close connection error", e);
        }
    }

    public static void close(Statement x) {
        if (x == null) {
            return;
        }
        try {
            x.close();
        } catch (Exception e) {
            boolean printError = true;
            if (e instanceof java.sql.SQLRecoverableException
                    && "Closed Connection".equals(e.getMessage())) {
                printError = false;
            }
            if (printError) {
                log.debug("close statement error", e);
            }
        }
    }

    public static void close(ResultSet x) {
        if (x == null) {
            return;
        }
        try {
            x.close();
        } catch (Exception e) {
            log.debug("close result set error", e);
        }
    }

    public static void close(Closeable x) {
        if (x == null) {
            return;
        }
        try {
            x.close();
        } catch (Exception e) {
            log.debug("close error", e);
        }
    }

    public static boolean isMysqlDbType(DbType dbType) {
        if (dbType == null) {
            return false;
        }
        switch (dbType) {
            case mysql:
            case oceanbase:
            case ads:
            case drds:
            case mariadb:
            case tidb:
            case h2:
                return true;
            default:
                return false;
        }
    }
}
